package com.m3958.vertxio.vertxsmsservice;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.sql.DataSource;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.vertx.java.core.Handler;
import org.vertx.java.core.MultiMap;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.platform.Verticle;


public class ServerExample  extends Verticle {
	
	/**
	 * create database vertxtest charset utf8;
	 * use vertxtest
	 * create table t (id bigint(20) not null,name varchar(255) default null);
	 * grant all privileges on vertxtest.* to 'vertxuser'@'localhost' identified by '1827837';
	 * @author libo
	 *
	 */
	public static class Person{
		private String email;
		
		private boolean enableAccessTk;

		public String getEmail() {
			return email;
		}

		public void setEmail(String email) {
			this.email = email;
		}

		public boolean isEnableAccessTk() {
			return enableAccessTk;
		}

		public void setEnableAccessTk(boolean enableAccessTk) {
			this.enableAccessTk = enableAccessTk;
		}
	}
	
	private String JDBC_URL = "jdbc:sqlserver://localhost;integratedSecurity=true;";
	//"jdbc:sqlserver://localhost;integratedSecurity=true;" 
	//"jdbc:mysql://127.0.0.1/vertxtest?characterEncoding=utf8"
	private String JDBC_USER = "vertxuser";
	
	private String JDBC_PWS = "1827837";
	

	  public void start() {
	    vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {
	      public void handle(HttpServerRequest req) {
	    	  
	    	  System.out.println("starting...");
	    	  req.response().headers().set("Content-Type", "text/html; charset=UTF-8");
	    	  
				MultiMap mm = req.params();
				Long mobile = null;
			
				try {
					mobile = Long.valueOf(mm.get("mo"));
				} catch (Exception e) {
					req.response().end("no mobile");
					return;
				}
				String msg = mm.get("msg");
			
				if(msg == null || msg.isEmpty() ){
					req.response().end("no message");
					return;
				}
				
			try {
				
				if (DbUtils.loadDriver("com.mysql.jdbc.Driver")) {
					Connection conn = DriverManager.getConnection(JDBC_URL,JDBC_USER,JDBC_PWS);
					QueryRunner runner=new QueryRunner();
					
					int inserts = runner.update(conn,"INSERT INTO Com_SmsSent_202 (MessageID,MessageContent,ExtendCode,DestinationAddress,SendType,SendTime) VALUES (?,?,?,?,?,?)",
							UUID.randomUUID().toString(),
							msg,
							"202",
							mobile,
							0,
							new Date(new java.util.Date().getTime()));
				
					if(inserts == 1){
						req.response().end("1");
					}else{
						req.response().end("0");
					}
					DbUtils.close(conn);
				}
			} catch (Exception e) {
				StringWriter sw = new StringWriter();
				PrintWriter pw = new PrintWriter(sw);
				e.printStackTrace(pw);
				System.out.println(sw.toString());
				req.response().end("0");
			}
	      }
	    }).listen(8083);
	  }

}